# Search for mercurial binary - hg
#
# The module defines the following variables:
#  Mercurial_HG_EXECUTABLE - path to svn command line client
#  Mercurial_HG_VERSION - version of svn command line client
#  Mercurial_FOUND - true if the command line client was found
#
# Copyright (c) 2009, Maciej "Fazi" Gałkowski <fazi.dev@gmail.com>

set(Mercurial_FOUND FALSE)

find_program(
    Mercurial_HG_EXECUTABLE hg
    DOC "hg command line client"
)
mark_as_advanced(Mercurial_HG_EXECUTABLE)

# Handle the QUIETLY and REQUIRED arguments and set Mercurial_FOUND to TRUE if 
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Mercurial DEFAULT_MSG Mercurial_HG_EXECUTABLE)

if(MERCURIAL_FOUND)
    set(Mercurial_FOUND TRUE)
    set(_Mercurial_SAVED_LC_ALL "$ENV{LC_ALL}")
    set(ENV{LC_ALL} C)

    execute_process(
        COMMAND ${Mercurial_HG_EXECUTABLE} --version
        OUTPUT_VARIABLE Mercurial_HG_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    string(
        REGEX REPLACE
        "Mercurial Distributed SCM \\(version ([.0-9]+).*\\).*"
        "\\1"
        Mercurial_HG_VERSION
        "${Mercurial_HG_VERSION}"
    )

    set(ENV{LC_ALL} ${_Mercurial_SAVED_LC_ALL})
endif(MERCURIAL_FOUND)

# FindMercurial.cmake ends here.